
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../api/UserApi.dart';
import '../../utils/SPutil.dart';
import '../../utils/Settings.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import '../../utils/Utils.dart';


class ChangeDefaultPwdPage extends StatefulWidget {
  const ChangeDefaultPwdPage({super.key});

  @override
  State<ChangeDefaultPwdPage> createState() => _ChangeDefaultPwdState();
}

class _ChangeDefaultPwdState extends State<ChangeDefaultPwdPage> with SingleTickerProviderStateMixin {


  String accountNo = "";
  String regExp = r'^(?![A-Za-z0-9]+$)(?![a-z0-9\W]+$)(?![A-Za-z\W]+$)(?![A-Z0-9\W]+$)[^\s]{8,16}$';

  @override
  void initState() {
    accountNo = Get.arguments["accountNo"];
    super.initState();
  }

  @override
  void dispose() {

    pwdController.dispose();
    pwdController2.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Settings.backgroundColor,

        body:SingleChildScrollView(
          child: Stack(
            clipBehavior: Clip.none,
            children: [
              SizedBox(
                height:MediaQuery.of(context).size.height,
                width: MediaQuery.of(context).size.width,
                child: Image.asset("images/logo_bg.png",fit: BoxFit.fill,),
              ),
              Center(
                child: Padding(
                    padding: const EdgeInsets.only(top: 160),
                    child: Column(
                      mainAxisSize: MainAxisSize.max,//表示Row在主轴(水平)方向占用的空间，默认是MainAxisSize.max，表示尽可能多的占用水平方向的空间，
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Image.asset("images/zhongfu-logo.png",width: 200,height:100,),
                        const Text("不能通过默认密码登录，请先修改密码",style: TextStyle(fontSize: 14.0,fontWeight: FontWeight.bold),),
                        Container(
                          alignment: Alignment.center,
                          padding: const EdgeInsets.only(top:80,left:30.0,right: 30.0),
                          margin: const EdgeInsets.only(bottom: 10.0),
                          child: Container(
                            child: buildForm(),
                          ),
                        ),


                      ],
                    )
                ),
              )
            ],
          ),
        ),

    );
  }


  /* 定义两个container类 unameController和pwdController*/

  TextEditingController pwdController = TextEditingController();
  TextEditingController pwdController2 = TextEditingController();
  GlobalKey formKey = GlobalKey<FormState>();
  Widget buildForm(){
    return Form(
      key: formKey,

      child: Column(
        children: [
          TextFormField(
            autofocus: false,
            controller: pwdController,
            obscureText: true, //密码输入类型
            decoration: const InputDecoration(
                labelText: "密码",
                hintText: "请输入密码",
                icon: Icon(Icons.lock)
            ),
            validator: (v){
              if(v==null || v.isEmpty || v.trim().isEmpty){
                return '请输入密码';
              }else if(!RegExp(regExp).hasMatch(v.toString())){
                return '密码必须8-16位且包含数字+特殊字符+英文字母大小写!';
              }
            },
          ),
          TextFormField(
            autofocus: false,
            controller: pwdController2,
            obscureText: true, //密码输入类型
            decoration: const InputDecoration(
                labelText: "确认密码",
                hintText: "请输入密码",
                icon: Icon(Icons.lock)
            ),
            validator: (v){
              if(v==null || v.isEmpty || v.trim().isEmpty){
                return '请输入密码';
              }else if(!RegExp(regExp).hasMatch(v.toString())){
                return '密码必须8-16位且包含数字+特殊字符+英文字母大小写!';
              }
            },
          ),

          Padding(
              padding: const EdgeInsets.only(top: 40.0),
              child: Row(
                children: [
                  Expanded(
                      child:SizedBox(
                        height: 50,
                        width: MediaQuery.of(context).size.width * 0.95,
                        child: OutlinedButton(
                          style: ButtonStyle(
                            backgroundColor: MaterialStateProperty.resolveWith<Color>((states) {
                              return Settings.primaryColor;
                            }),
                            foregroundColor: MaterialStateProperty.resolveWith<Color>((states) {
                              return Colors.white;
                            }),
                          ),
                          onPressed:submit,
                          child: const Text('确定',style: TextStyle(fontSize: 17,)),
                        ),
                      )

                  )
                ],
              )
          ),

        ],
      ),
    );
  }



  submit() async {

    if((formKey.currentState as FormState).validate()){
      if(pwdController.text!=pwdController2.text){
        EasyLoading.showError("两次密码输入不一致");
        return;
      }

      EasyLoading.show(status: "密码修改中...");
      Map formData={"accountNo":accountNo,"empPwd":Utils.generateMD5(pwdController.text)};
      dynamic res =  await UserApi.changeDefaultPwd(formData);
      if(res["success"]){
        EasyLoading.dismiss();
        //否则会有 DON'T use BuildContext across asynchronous gaps. 警告
        if(mounted){
          Get.offAllNamed("/userLogin");
        }
      }else{
        EasyLoading.dismiss();
        EasyLoading.showError(res["message"]);
      }
    }
  }
}


